package com.breaktian.assemble.customviews.viewpager.bannerviewpager;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.breaktian.assemble.schedule.CommonTask;
import com.breaktian.assemble.schedule.ScheduleUtils;

/**
 * Created by tiankeke on 2017/6/14.
 */

public class BannerViewPager extends ViewPager{

    private boolean isScrollEnabled = true;//默认是true
    private int dataSize;
    private static final int TIME = 3000;//3s
    Handler mHandler = new Handler();
    private CommonTask task;
    private PageDotView mDotView;


    public BannerViewPager(Context context) {
        super(context);
        init();
    }

    public BannerViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    private void init() {

    }

    public void initPosition(){
        int currPostion = Integer.MAX_VALUE/2;
        for(int i=0;i<dataSize;i++){
            if(--currPostion%dataSize==0){
                setCurrentItem(currPostion);
                break;
            }
        }
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        super.setAdapter(adapter);
        dataSize = BannerPagerAdapter.DATA_SIZE;
        initPosition();
        startAutoScroll();
    }

    public void bindDotView(PageDotView dotView){
        this.mDotView = dotView;
        mDotView.updatePosition(getCurrentItem()%mDotView.dotSize);
        addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(mDotView!=null){
                    mDotView.updatePosition(position%mDotView.dotSize);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    /**
     * 开始轮播
     * */
    public void startAutoScroll() {
        if(task!=null&&task.isRunning()){
            return;
        }
        //滚动task
        task = new CommonTask() {
            @Override
            protected void doTask() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        setCurrentItem(getCurrentItem()+1);
                    }
                });
            }
        };

        ScheduleUtils.get().startSchedule(task,TIME, TIME);
    }

    public void stopAutoScroll(){
        ScheduleUtils.get().stopSchedule(task);
    }

    public void setScrollEnabled(boolean isScrollEnabled){
        this.isScrollEnabled = isScrollEnabled;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                startAutoScroll();
                break;

        }
        return this.isScrollEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                stopAutoScroll();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                startAutoScroll();
                break;

        }
        return this.isScrollEnabled && super.onInterceptTouchEvent(event);
    }

}
